import ls from '@/utils/secureLS'

export default {
  namespaced: true,
  state: {
    user: undefined,
    permissions: null,
    roles: null,
    menus: null,
    routesConfig: null,
    lockPwd: '',
    lockFlag: ''
  },
  getters: {
    user: state => {
      // console.log(state)
      // if (!state.user) {
      //   try {
      //     const user = localStorage.getItem(process.env.VUE_APP_USER_KEY)
      //     state.user = JSON.parse(user)
      //   } catch (e) {
      //     console.error(e)
      //   }
      // }
      try {
        state.user = ls.get(process.env.VUE_APP_USER_KEY)
      } catch (e) {
        state.user = ''
      }

      return state.user
    },
    permissions: state => {
      // if (!state.permissions) {
      //   try {
      //     const permissions = localStorage.getItem(process.env.VUE_APP_PERMISSIONS_KEY)
      //     state.permissions = JSON.parse(permissions)
      //     state.permissions = state.permissions ? state.permissions : []
      //   } catch (e) {
      //     console.error(e.message)
      //   }
      // }
      try {
        state.permissions = ls.get(process.env.VUE_APP_PERMISSIONS_KEY)
      } catch (e) {
        state.permissions = ''
      }
      return state.permissions
    },
    roles: state => {
      // if (!state.roles) {
      //   try {
      //     const roles = localStorage.getItem(process.env.VUE_APP_ROLES_KEY)
      //     state.roles = JSON.parse(roles)
      //     state.roles = state.roles ? state.roles : []
      //   } catch (e) {
      //     console.error(e.message)
      //   }
      // }
      try {
        state.roles = ls.get(process.env.VUE_APP_ROLES_KEY)
      } catch (e) {
        state.roles = ''
      }
      return state.roles
    },
    menus: state => {
      // if (!state.menus) {
      //   try {
      //     const menus = localStorage.getItem(process.env.VUE_APP_MENUS_KEY)
      //     state.menus = JSON.parse(menus)
      //     state.menus = state.menus ? state.menus : []
      //   } catch (e) {
      //     console.error(e.message)
      //   }
      // }
      try {
        state.menus = ls.get(process.env.VUE_APP_MENUS_KEY)
      } catch (e) {
        state.menus = ''
      }
      return state.menus
    },
    routesConfig: state => {
      if (!state.routesConfig) {
        try {
          const routesConfig = localStorage.getItem(process.env.VUE_APP_ROUTES_KEY)
          state.routesConfig = JSON.parse(routesConfig)
          state.routesConfig = state.routesConfig ? state.routesConfig : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.routesConfig
    },
    lockPwd: state => {
      try {
        state.lockPwd = ls.get(process.env.VUE_APP_LOCK_PWD)
      } catch (e) {
        state.lockPwd = ''
      }
      return state.lockPwd
    },
    lockFlag: state => {
      try {
        state.lockFlag = ls.get(process.env.VUE_APP_LOCK_FLAG)
      } catch (e) {
        state.lockFlag = ''
      }
      return state.lockFlag
    }
  },
  mutations: {
    setUser (state, user) {
      state.user = user
      ls.set(process.env.VUE_APP_USER_KEY, user)
      // localStorage.setItem(process.env.VUE_APP_USER_KEY, JSON.stringify(user))
    },
    setMenus (state, menus) {
      state.menus = menus
      ls.set(process.env.VUE_APP_MENUS_KEY, menus)
      // localStorage.setItem(process.env.VUE_APP_MENUS_KEY, JSON.stringify(menus))
    },
    setPermissions (state, permissions) {
      state.permissions = permissions
      ls.set(process.env.VUE_APP_PERMISSIONS_KEY, permissions)
      // localStorage.setItem(process.env.VUE_APP_PERMISSIONS_KEY, JSON.stringify(permissions))
    },
    setRoles (state, roles) {
      state.roles = roles
      ls.set(process.env.VUE_APP_ROLES_KEY, roles)
      // localStorage.setItem(process.env.VUE_APP_ROLES_KEY, JSON.stringify(roles))
    },
    setRoutesConfig (state, routesConfig) {
      state.routesConfig = routesConfig
      localStorage.setItem(process.env.VUE_APP_ROUTES_KEY, JSON.stringify(routesConfig))
    },
    setLockPwd (state, lockPwd) {
      state.lockPwd = lockPwd
      ls.set(process.env.VUE_APP_LOCK_PWD, lockPwd)
    },
    setLockFlag (state, lockFlag) {
      state.lockFlag = lockFlag
      ls.set(process.env.VUE_APP_LOCK_FLAG, lockFlag)
    }
  }
}
